<template>
  <div class="layout-container">
    <!-- 导航栏 --->
    <el-header class="layout-header">
      <div class="header-content">
        <!-- logo --->
        <div class="logo">
          <h2>音乐管理系统</h2>
        </div>

        <!-- 管理员信息 --->
        <div class="user-info">
          <el-dropdown @command="handleUserCommand">
            <div class="user-avatar">
              <el-avatar :icon="User" />
              <span class="username">{{ adminName }}</span>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="logout">
                  <el-icon><SwitchButton /></el-icon>
                  <span>退出登录</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-header>

    <el-container class="layout-main">
      <el-aside class="layout-aside" width="200px">

        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          background-color="545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          router
          @select="handleMenuSelect"
        >
          <el-menu-item index="/dashboard">
            <el-icon><Odometer /></el-icon>
            <span>数据统计</span>
          </el-menu-item>
          <el-menu-item index="/consumerList">
            <el-icon><UserFilled /></el-icon>
            <span>用户管理</span>
          </el-menu-item>
          <el-menu-item index="/singer">
            <el-icon><UserFilled /></el-icon>
            <span>歌手管理</span>
          </el-menu-item>
          <el-menu-item index="/rank">
            <el-icon><Trophy /></el-icon>
            <span>榜单管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-main class="layout-content">
        <router-view />
      </el-main>
    </el-container>
  </div>
</template>

<script>
import { ref,computed } from 'vue'
import {useRouter,useRoute} from "vue-router"
import { ElMessage,ElMessageBox } from "element-plus";
import {Odometer, SwitchButton,UserFilled, Trophy} from "@element-plus/icons-vue"
import api from '@/services/api'

export default {
  name: 'layout',
  components: {
    Odometer,
    SwitchButton,
    UserFilled,
    Trophy
  },
  setup(){
    const router = useRouter()
    const route = useRoute()

    const adminName = ref(localStorage.getItem('adminName') || '管理员')

    const activeMenu = computed(() => {
      const { meta, path } = route

      if(meta.activeMenu){
        return meta.activeMenu
      }
      return path
    })

    const handleUserCommand = async (command) => {
      if(command === 'logout'){
        try{
          // 确认退出登录
          await ElMessageBox.confirm('确定要退出登录吗？','提示',{
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })

          // 退出登录
          await api.admin.logout()

          // 删除token
          localStorage.removeItem('adminToken')
          localStorage.removeItem('adminName')

          ElMessage.success('退出登录成功')

          // 跳转到登录页面
          router.push('/home')
        } catch (error) {
          if(error !== cancel){
            ElMessage.error('退出登录失败')
          }
        }
        }
      }

      // 处理菜单选择
      const handleMenuSelect = (index) => {

        console.log("已选择菜单:",index)
      }

      return {
        adminName,
        activeMenu,
        handleUserCommand,
        handleMenuSelect
      }
    }
  }


</script>

<style scoped>.layout-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.layout-header {
  background-color: #ffffff;
  color: #333;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  z-index: 100;
  padding: 0;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 20px;
}

.logo h2 {
  margin: 0;
  color: #409eff;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-avatar {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin-left: 10px;
  font-size: 14px;
}

.layout-main {
  flex: 1;
  overflow: hidden;
}

.layout-aside {
  background-color: #545c64;
  height: 100%;
}

.sidebar-menu {
  height: 100%;
  border-right: none;
}

.sidebar-menu :deep(.el-menu-item) {
  height: 50px;
  line-height: 50px;
}

.sidebar-menu :deep(.el-menu-item.is-active) {
  background-color: #434a50 !important;
}

.layout-content {
  background-color: #f5f5f5;
  padding: 20px;
  overflow: auto;
  height: calc(100vh - 60px);
}
</style>